package com.eascs.partner.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.partner.form.PartnerQueryForm;
import com.eascs.partner.service.PartnerService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.service.sys.SysPermissionService;
import com.eascs.workflow.common.vo.ModelResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/partner")
public class PartnerController extends BaseMasterViewController {
	
	@Reference
	SysOrgService sysOrgService;
	
	@Autowired
	PartnerService partnerService;
	
	@Reference
	SysPermissionService sysPermissionService;
	
	/**
	 * @param params
	 * 
	 */
	@RequestMapping("/index")
	public String findPartnerInfo(PartnerQueryForm params) {

		params.setPageSize(10); // 每页数据量

		/**
		 * @param 拼接转页时的URL；
		 * 
		 */
		String o2ourl = "/partner/index";
		String partnerName = params.getPartnerName() == null ? "" : params.getPartnerName();
		String brandName = params.getBrandName() == null ? "" : params.getBrandName();
		String hostProvince = params.getHostProvince() == null ? "" : params.getHostProvince();
		String createTime = params.getCreateTime() == null ? "" : params.getCreateTime();
		String createEndTime = params.getCreateEndTime() == null ? "" : params.getCreateEndTime();
		String operator = params.getOperator() == null ? "" : params.getOperator();
		
		String Status = params.getStatus() == null ? "" : params.getStatus();
		o2ourl = UrlUtil.appendUrl(o2ourl, "partnerName", partnerName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "brandName", brandName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "hostProvince", hostProvince);
		o2ourl = UrlUtil.appendUrl(o2ourl, "createTime", createTime);
		o2ourl = UrlUtil.appendUrl(o2ourl, "createEndTime", createEndTime);
		o2ourl = UrlUtil.appendUrl(o2ourl, "Status", Status);
		o2ourl = UrlUtil.appendUrl(o2ourl, "operator", operator);
		o2ourl = o2ourl + "&pageIndex=";
		/**
		 * @param 查询相关信息，放入页面相关数据
		 * 
		 */
		PageResult<Object[]> pageResult = partnerService.findPartnerInfoByPage(params, params.getPageIndex(),params.getPageSize());
		String[] titles = { "申报单号","经办人", "主办省份", "渠道名称",  "所属品牌", "融资额度(万元)", "创建时间", "状态" };
		SelectDataOption[] selectDataOptions = { null, null,new SelectDataOption("O2OPROVINCE"),null, null, null,null, new SelectDataOption("parWFStutus") };
		Button[] Button = new Button[] { ButtonHelper.createAjaxButton(null, "/rpartner/view_detail?parameter="), null };
		Button delete = ButtonHelper.createAjaxConfirmButton("删除", "/rpartner/delete?parameter=", "editPartner");
		delete.setPermissionCode("deletePartner");
		delete.setShowButtonFilter(new ShowButtonFilter() {
			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				if ("-1".equals(params[8] + "") || "3".equals(params[8] + ""))
					return true;
				else
					return false;
			}
		});
		Button startFlow = ButtonHelper.createAjaxButton("发起流程", "/rpartner/startFlow?id=", "startPartnerFlow");// parameter为合作方id
		startFlow.setShowButtonFilter(new ShowButtonFilter() {
			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				if ("-1".equals(params[8] + "") || "3".equals(params[8] + ""))
					return true;
				else
					return false;
			}
		});
		User user = UserHolder.getUser();
		Button edit = ButtonHelper.createAjaxButton("编辑", "/rpartner/view_edit?parameter=", "editPartner");// parameter为合作方id
		edit.setShowButtonFilter(new ShowButtonFilter() {
			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				if ("-1".equals(params[8] + "") || "3".equals(params[8] + "")) {
					if (user.getNo().equals(params[9] + "") || user.isSuperAdministrator()) {
						return true;
					}
				} else if ("0".equals(params[8]+"")) {
					ModelResult<Boolean> result = sysPermissionService.checkAuth(user.getNo(), "ZBPPSH");
					if ( result.isSuccess() && result.getModel()) {
						return true;
					}
				}
					return false;
			}
		});
		//Button[] titleButtons = new Button[] { edit, delete, startFlow };
		
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, o2ourl);
		//paginationInfo.setMoreButtons(titleButtons);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setBuildSelectBox(false);
		paginationInfo.setTitleButtons(Button);
		paginationInfo.setSelectDataOptions(selectDataOptions);
		List<ActOrgEntity> orgProvince = sysOrgService.getOrgProvince();
		ViewContext.put("ActOrgEntitys", orgProvince);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("partner", params);
		ViewContext.put("Status", params.getStatus());

		return "partner/index";

	}
	
	@Override
	public void preHandle() {
		// TODO Auto-generated method stub
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "partner");
	}

}
